package com.xiaoc.bulldozer.controller;

import com.google.common.collect.Maps;
import com.xc.framework.common.page.PageDto;
import com.xiaoc.bulldozer.common.dto.AppointmentQueryDto;
import com.xiaoc.bulldozer.common.dto.StatisticUserQueryDto;
import com.xiaoc.bulldozer.common.model.Appointment;
import com.xiaoc.bulldozer.common.service.AppointmentService;
import com.xiaoc.bulldozer.common.util.ReportServiceImpl;
import com.xiaoc.bulldozer.common.util.ReportTemplate;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wj
 */
@Controller
@RequestMapping(value = "/statistic")
public class StatisticController extends AdminBaseController {

    private Logger logger = LoggerFactory.getLogger(StatisticController.class);

    @Resource(name = "appointmentService")
    private AppointmentService appointmentService;

    @RequestMapping(value = "/appointmentList", method = RequestMethod.GET)
    public ModelAndView internetBankList(AppointmentQueryDto queryDto) throws ParseException {
        if (queryDto.getPage() == null){
            queryDto.setPage(1);
        }

        Map<String, Object > param = Maps.newHashMap();
        int count = appointmentService.getAppointmentCountByCondition(queryDto);
        List<Appointment > appointmentList = appointmentService.getAppointmentListByCondition(queryDto);
        param.put("pageDto", new PageDto(queryDto.getPage(), count, PageDto.PAGE_SIZE));
        param.put("queryDto", queryDto);
        param.put("appointmentList", appointmentList);

        return renderHtml("statistic/appointmentList.html", param);
    }

    /**
     * 导出报表
     *
     * @return
     */
    @RequestMapping(value = "/appointmentExport", method = RequestMethod.GET)
    public void appointmentExport(AppointmentQueryDto queryDto, HttpServletResponse response) throws Exception {

        String[] headers = {
                "transactorPhone",
                "linkmanPhone",
                "linkmanName",
                "createTime",
        };

        Map<String, String> map = Maps.newHashMap();
        map.put("transactorPhone", "预约人手机号");
        map.put("linkmanPhone", "预约人手机号");
        map.put("linkmanName", "预约人手机号");
        map.put("createTime", "预约时间");

        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");

        String sheetName = "预约报表";
        String excelName = sheetName + df.format(new Date()) + ".xls";

        //生成报表
        queryDto.setOffset(0);
        queryDto.setLimit(Integer.MAX_VALUE);
            int count = appointmentService.getAppointmentCountByCondition(queryDto);
            List<Appointment> appointmentList = appointmentService.getAppointmentListByCondition(queryDto);

            new ReportTemplate<Appointment>() {

                @Override
                public void setParam() {
                    this.setName(excelName);
                    this.setResponse(response);
                }

                @Override
                public void setData() {
                    HSSFSheet sellSheet = this.getWorkbook().createSheet("预约信息");
                    new ReportServiceImpl<Appointment>().exportSellExcel(appointmentList, this.getWorkbook(), headers, sellSheet, map);
                }

            }.createReport();

    }

    /**
     * 运营成本核算报表
     */
    @RequestMapping(value = "/costList", method = RequestMethod.GET)
    public ModelAndView costList(StatisticUserQueryDto queryDto) {

        Map<String, Object> params = new HashMap<String, Object>();


        return renderHtml("statistic/costList.html", params);
    }


    public static void main(String[] args) {
        float a = 100 * 1.00f /99 * 1.00f;
        System.out.println(a);
        System.out.println(String.format("%.2f", 0.08));
        System.out.println(String.format("%.2f", 0.08888));
        System.out.println(String.format("%.2f", 0.08188));

    }

}
